export * from './bluetooth';
export * from './remoteBluetooth';
export * from './constant';

export function throttle<T extends (...args: any[]) => any>(
  func: T,
  delay: number
): (...args: Parameters<T>) => void {
  let lastExecTime = 0
  let timeoutId: ReturnType<typeof setTimeout> | null = null

  return function (this: any, ...args: Parameters<T>) {
    const currentTime = Date.now()
    const timeSinceLastExec = currentTime - lastExecTime
    const remainingTime = delay - timeSinceLastExec

    if (timeoutId) {
      clearTimeout(timeoutId)
    }

    if (remainingTime <= 0) {
      // 如果距离上次执行已经超过delay时间，立即执行
      lastExecTime = currentTime
      func.apply(this, args)
    } else {
      // 否则设置定时器，在剩余时间后执行
      timeoutId = setTimeout(() => {
        lastExecTime = Date.now()
        func.apply(this, args)
      }, remainingTime)
    }
  }
}